#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QPainter>
#include <QMouseEvent>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    {
       resize(640, 640);
       memset(a, 0, 15 * 15 * sizeof(int));
       player = 0;
   }
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::paintEvent(QPaintEvent*)
{
    QPainter p(this);
    p.setRenderHint(QPainter::Antialiasing, true);
    int i, j;
    for (i = 0; i < 16; i++)
    {
        p.drawLine(20, 20 + i * 40, 620, 20 + i * 40);
        p.drawLine(20 + i * 40, 20, 20 + i * 40, 620);
    }
    QBrush brush;
    brush.setStyle(Qt::SolidPattern);
    for (i = 0; i < 15; i++)
    {
        for (j = 0; j < 15; j++)
        {
            if (a[i][j] == 1)
            {
                brush.setColor(Qt::black);
                p.setBrush(brush);
                p.drawEllipse(QPoint((i + 1) * 40, (j + 1) * 40), 15, 15);
            }
            else if (a[i][j] == 2)
            {
                brush.setColor(Qt::white);
                p.setBrush(brush);
                p.drawEllipse(QPoint((i + 1) * 40, (j + 1) * 40), 15, 15);
            }
        }
    }
 
}

int MainWindow::isWin(int x, int y)
{
    return f1(x, y) || f2(x, y) || f3(x, y) || f4(x, y);
}

int MainWindow::f1(int x, int y)
{
    int i;
    for (i = 0; i < 5; i++)
    {
        if (y - i >= 0 &&
            y + 4 - i <= 0xF &&
            a[x][y - i] == a[x][y + 1 - i] &&
            a[x][y - i] == a[x][y + 2 - i] &&
            a[x][y - i] == a[x][y + 3 - i] &&
            a[x][y - i] == a[x][y + 4 - i])
            return 1;
    }
    return 0;
}

int MainWindow::f2(int x, int y)
{
    int i;
    for (i = 0; i < 5; i++)
    {
        if (x - i >= 0 &&
            x + 4 - i <= 0xF &&
            a[x - i][y] == a[x + 1 - i][y] &&
            a[x - i][y] == a[x + 2 - i][y] &&
            a[x - i][y] == a[x + 3 - i][y] &&
            a[x - i][y] == a[x + 4 - i][y])
            return 1;
    }
    return 0;
}
